import React from 'react';
import {injectIntl} from './intl';
import {Table} from 'antd';
import PropTypes from 'prop-types';
import routes from './routes';
import {
    PAGE,
    TABLE,
    PAGINATION_OPTIONS,
    COMMON_TABLE_QUERY_FAIL_TEXT,
    COMMON_TABLE_EMPTY_TEXT,
    DATE_FORMAT,
} from '../constants';
import {Link} from 'react-router-dom';
import {partClaimStatusHis} from '../Enum';
import {conventEnumValueToString, formatDateTime, conventSorter} from '../utils';

export class HistoryTablePanel extends React.PureComponent {
    state = {
        loading: false
    };

    onChangeSort = (pagination, filters, sorter) => {
        if(pagination.current - 1 === this.props.pageIndex && pagination.pageSize === this.props.pageSize)
            this.props.onChangeSort({...conventSorter(sorter),
                pageIndex: PAGE.index});
    };
    render() {
        const columns = [
            {
                title: this.props.intl.formatMessage({
                    id: 'historyTablePanel.column.code',
                    defaultMessage: '结算单号'
                }),
                sorter: true,
                dataIndex: 'code',
                render: (text, record) => (
                    <Link key="historyDetail" to={routes.historyDetail.format(record.id)}>
                        {text}
                    </Link>
                )
            },
            {
                title: this.props.intl.formatMessage({
                    id: 'historyTablePanel.column.uploadDate',
                    defaultMessage: 'PI上传时间'
                }),
                dataIndex: 'uploadDate',
                sorter: true,
                render: text => formatDateTime(text, DATE_FORMAT)
            },
            
            {
                title: this.props.intl.formatMessage({
                    id: 'historyTablePanel.column.status',
                    defaultMessage: '状态'
                }),
                dataIndex: 'status',
                sorter: true,
                render: text => conventEnumValueToString(partClaimStatusHis, text)
            }
        ];
        if(!this.props.isDealerUser)
            columns.splice(1, 0,
                {
                    title: this.props.intl.formatMessage({
                        id: 'historyTablePanel.column.dealerCode',
                        defaultMessage: '总代编号'
                    }),
                    dataIndex: 'dealerCode',
                    sorter: true,
                },
                {
                    title: this.props.intl.formatMessage({
                        id: 'historyTablePanel.column.dealerName',
                        defaultMessage: '总代名称'
                    }),
                    dataIndex: 'dealerName',
                });
        const pagination = {
            total: this.props.total,
            pageSize: this.props.pageSize,
            current: this.props.pageIndex + 1,
            onShowSizeChange: this.props.onPageSizeChange,
            onChange: this.props.onPageIndexChange,
            ...PAGINATION_OPTIONS
        };
        const locale = {
            emptyText: this.props.errorMessage ? COMMON_TABLE_QUERY_FAIL_TEXT : COMMON_TABLE_EMPTY_TEXT
        };
        return (
            <div>
                <Table
                    className="white-space-nowrap"
                    columns={columns}
                    dataSource={this.props.data}
                    rowKey="id"
                    pagination={pagination}
                    loading={this.props.loading || this.state.loading}
                    onChange={this.onChangeSort}
                    locale={locale}
                    {...TABLE}/>
            </div>
        );
    }
}
HistoryTablePanel.propTypes = {
    data: PropTypes.array.isRequired,
    errorMessage: PropTypes.string.isRequired,
    loading: PropTypes.bool.isRequired,
    total: PropTypes.number.isRequired,
    onChangeSort: PropTypes.func.isRequired,
    onPageIndexChange: PropTypes.func.isRequired,
    onPageSizeChange: PropTypes.func.isRequired,
    intl: PropTypes.object,
    pageIndex: PropTypes.number,
    pageSize: PropTypes.number
};
import {connect} from 'react-redux';
import selectorFactory from 'Shared/utils/immutableToJsSelectorFactory';
import {
    tableHistorySearch,
} from './actions';
const getData = selectorFactory(['page', 'domainData', 'historyList', 'data']);
const getDealerUser = selectorFactory(['page', 'domain', 'initData', 'isDealerUser']);
const mapStateToProps = state => ({
    data: getData(state),
    loading: state.getIn(['page', 'domainData', 'historyList', 'isFetching']),
    errorMessage: state.getIn(['page', 'domainData', 'historyList', 'message']),
    total: state.getIn(['page', 'domainData', 'historyList', 'total']),
    pageIndex: state.getIn(['page', 'appState', 'historyQueryBySort', 'pageIndex']),
    pageSize: state.getIn(['page', 'appState', 'historyQueryBySort', 'pageSize']),
    isDealerUser: getDealerUser(state),
});

const mapDispatchToProps = dispatch => ({
    onPageSizeChange: (pageIndex, pageSize) =>
        dispatch(
            tableHistorySearch({
                pageIndex: PAGE.index,
                pageSize
            })
        ),
    onPageIndexChange: pageIndex =>
        dispatch(
            tableHistorySearch({
                pageIndex: pageIndex - 1
            })
        ),
    onChangeSort: option => dispatch(tableHistorySearch(option)),
});

export default connect(
    mapStateToProps,
    mapDispatchToProps
)(injectIntl(HistoryTablePanel));
